home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / O Boy / Source / AppAEObj_pd.cp < prev    next >
Encoding:
Text File  |  1996-06-21  |  5.6 KB  |  222 lines  |  [TEXT/R*ch]

  1. /*
  2.     AppAEObj_pd.h
  3.     © Bob Boylan 1996
  4.  
  5.     Revision History
  6.     MacHack 1996    initial creation
  7. */
  8. #include "debug.h"
  9. #include "AppAEObj_pd.h"
  10. #include "PropertyValue_pd.h"
  11. #include "ComputerAEObj_pd.h"
  12. #include "Helpers_ut.h"
  13. #include "AEBuild.h"
  14.  
  15. #include <string.h>
  16. #include <sstream>
  17.  
  18.  
  19.  
  20. //    -----------------------------------------------------------------
  21. //    ctor
  22. //
  23. AppAEObj_pd::AppAEObj_pd( AEObj_pd * inParent, DescType inKind, Int_32 inIndex )
  24.     : AEObj_pd( inParent, inKind, inIndex )
  25. {
  26.     // get the finder info ... our parent is a computer
  27.         _SizeofFinderAppAddr = inParent->GetSizeofAppAddr();
  28.         dassert( _SizeofFinderAppAddr == sizeof(OSType) );
  29.         memcpy( &_FinderAppAddr, inParent->GetAppAddr(), _SizeofFinderAppAddr );
  30.         _FinderAppAddrType = inParent->GetAppAddrType();
  31.         _HaveAppAddr = false;
  32.  
  33.     // we don't want to be using the computer aete
  34.         Clone_ut< AETE_da >    theClonedAETE;
  35.         _AETE = theClonedAETE;
  36.  
  37. }
  38.  
  39. //    -----------------------------------------------------------------
  40. //    ctor from child
  41. //
  42. AppAEObj_pd::AppAEObj_pd( AEObj_pd * inChild )
  43.     : AEObj_pd( inChild )
  44. {
  45.     _HaveAppAddr = true;
  46.     _AETE = inChild->GetAETE();
  47.     
  48.     memcpy( &_AppSignature, inChild->GetAppAddr(), inChild->GetSizeofAppAddr() );
  49.  
  50. }
  51.  
  52. //    -----------------------------------------------------------------
  53. //    dtor
  54. //
  55. AppAEObj_pd::~AppAEObj_pd()
  56. {}
  57. //    -----------------------------------------------------------------
  58. //    Update
  59. //
  60. void
  61. AppAEObj_pd::Update( ProgressProc_hi &inProgressProc, Int_32 inMaxSubModels )
  62. {
  63.     // see if we need the app's aete ... we only get it when necessary
  64.     if( _HaveAppAddr == false )
  65.     {
  66.         PropertyValue_pd theCreator = GetProcessPropertyValue('fcrt');
  67.         if( (*theCreator._Value).mDesc.descriptorType == typeNull )
  68.         {
  69.             // dassert( false );    // app not there?
  70.         }
  71.         else
  72.         {
  73.             Handle theHandle = (*theCreator._Value).mDesc.dataHandle;
  74.             _AppSignature = **(OSType**) theHandle;
  75.             if( IsScriptable() == true )
  76.             {
  77.                 IsValidAETE();    // force validation (kinda parasitic)
  78.                 _HaveAppAddr = true;
  79.             }
  80.  
  81.         }
  82.     }
  83.     
  84.     // ok, now that we have the application address we can proceed normally
  85.     if( _HaveAppAddr == true )
  86.     {
  87.         AEObj_pd::Update( inProgressProc, inMaxSubModels );
  88.     }
  89.  
  90. }
  91.     
  92. //    -----------------------------------------------------------------
  93. //    GetParent
  94. //
  95. Clone_ut< AEObj_pd >
  96. AppAEObj_pd::GetParent()
  97. {
  98.     // our parent is always a computer
  99.     AEObj_pd    *theComputer = new ComputerAEObj_pd;
  100.     Clone_ut<AEObj_pd>    theClone( theComputer );
  101.     return theClone;
  102. }
  103.  
  104.  
  105. //    -----------------------------------------------------------------
  106. //    GetProcessPropertyValue
  107. //
  108. PropertyValue_pd
  109. AppAEObj_pd::GetProcessPropertyValue( DescType inKind )
  110. {
  111.     // ask the finder for this value
  112.         ostringstream    theStream( ios::in | ios::out );
  113.         // create the aegizmo string
  114.         theStream << "'----':obj {form:prop," <<
  115.                                  "want:type(prop)," <<
  116.                                  "seld:type(" << As4CharString( inKind ) << ")," <<
  117.                                  "from:" << 
  118.                                        "obj {want:type(pcap)," <<
  119.                                             "form:indx,seld:" << _ObjectSpec.back()._Pos <<
  120.                                             ",from:'null'()}" <<
  121.                                         "}";
  122.         OSErr theErr;
  123.         StAEDescriptor    theAppleEvent;
  124.         // use gizmos to create the apple event
  125.          theErr = AEBuildAppleEvent( kAECoreSuite, kAEGetData,
  126.                     GetFinderAppAddrType(),
  127.                     GetFinderAppAddr(),
  128.                     GetSizeofFinderAppAddr(),
  129.                     kAutoGenerateReturnID, kAnyTransactionID,
  130.                     &theAppleEvent.mDesc, theStream.str().c_str() );
  131.         dassert( theErr == noErr );
  132.         
  133.         StAEDescriptor    theReplyAppleEvent;
  134.         // and off with it
  135.         theErr = AESend( &theAppleEvent.mDesc, &theReplyAppleEvent.mDesc, kAEWaitReply,
  136.                         kAENormalPriority, kAEDefaultTimeout, nil, nil );
  137.  
  138.         // package up the return value
  139.         StAEDescriptor    *theResult = new StAEDescriptor;
  140.         Clone_ut<StAEDescriptor>    theVal( theResult );
  141.         if( theErr == noErr )
  142.         {
  143.             theErr = AEGetParamDesc( theReplyAppleEvent, keyDirectObject, typeWildCard, &theResult->mDesc );
  144.         }
  145.         PropertyValue_pd theRetVal( inKind, theVal, string("") );
  146.         
  147.         return theRetVal;
  148.  
  149. }
  150. //    -----------------------------------------------------------------
  151. //    GetFinderAppAddr
  152. //
  153. void *
  154. AppAEObj_pd::GetFinderAppAddr()
  155. {
  156.     return &_FinderAppAddr;
  157. }
  158. //    -----------------------------------------------------------------
  159. //    GetFinderAppAddrType
  160. //
  161. OSType
  162. AppAEObj_pd::GetFinderAppAddrType()
  163. {
  164.     return typeApplSignature;
  165. }
  166. //    -----------------------------------------------------------------
  167. //    GetSizeofFinderAppAddr
  168. //
  169. Int_32
  170. AppAEObj_pd::GetSizeofFinderAppAddr()
  171. {
  172.     return sizeof(OSType);
  173. }
  174.  
  175. //    -----------------------------------------------------------------
  176. //    IsScriptable
  177. //
  178. Boolean
  179. AppAEObj_pd::IsScriptable()
  180. {
  181.     // ask the finder
  182.     PropertyValue_pd isScriptableProp = GetProcessPropertyValue('isab');
  183.     return AsBoolean( isScriptableProp );
  184. }
  185.  
  186. //    -----------------------------------------------------------------
  187. //    IsValidAETE
  188. //
  189. Boolean
  190. AppAEObj_pd::IsValidAETE()
  191. {
  192.     if( (_AETE.Isnil() == true) && (_HaveAppAddr == false) )
  193.     {
  194.         AETE_da *theAETE = new AETE_da( this );
  195.         Clone_ut< AETE_da >    theClonedAETE( theAETE );
  196.         _AETE = theClonedAETE;
  197.     }
  198.     return !(_AETE.Isnil());
  199.  
  200. }
  201.  
  202. //    -----------------------------------------------------------------
  203. //    GetName
  204. //
  205. string
  206. AppAEObj_pd::GetName()
  207. {
  208.     // our formal name
  209.     ostringstream    theStream( ios::in | ios::out );
  210.     theStream << "App" << " # " << _ObjectSpec.back()._Pos ;
  211.     _ObjectSpec.back()._FormalName = theStream.str();
  212.     
  213.     // and our regular name
  214.     PropertyValue_pd theName = GetProcessPropertyValue(pName);
  215.     if( (*theName._Value).mDesc.descriptorType != typeNull )
  216.     {
  217.         _ObjectSpec.back()._Name =  Asstring( theName );
  218.         theStream <<  string(" (") << _ObjectSpec.back()._Name << string(")");
  219.     }
  220.     return theStream.str();
  221. }
  222.